﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Model.Entities;

namespace Model.DAL
{
    public class WinterContext : DbContext
    {
        public DbSet<Page> Pages { get; set; }
        public DbSet<Module> Modules { get; set; }
        public DbSet<Theme> Themes { get; set; }
        public DbSet<PageModuleSettings> PageModuleSettings { get; set; }

        public virtual void Commit()
        {
            base.SaveChanges();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseIfModelChanges<WinterContext>());

            modelBuilder.Entity<Page>().HasRequired(p => p.Theme);
            modelBuilder.Entity<Page>().HasMany(p => p.ChildPages).WithOptional(p => p.ParentPage).HasForeignKey(p => p.ParentID);   

            modelBuilder.Entity<PageModuleSettings>().HasRequired(pm => pm.Module).WithMany(m=>m.PageModuleSettings).HasForeignKey(pm=>pm.ModuleID);
            modelBuilder.Entity<PageModuleSettings>().HasRequired(pm => pm.Page).WithMany(p => p.PageModuleSettings).HasForeignKey(pm => pm.PageID);

            #region THEME Entity
            modelBuilder.Entity<Theme>().HasKey(t => t.ID);
            modelBuilder.Entity<Theme>().Property(t => t.ID).HasColumnName("theme_id").HasColumnType("int");
            modelBuilder.Entity<Theme>().Property(t => t.Name).HasColumnName("theme_name").HasColumnType("nvarchar").HasMaxLength(50);
            modelBuilder.Entity<Theme>().Property(t => t.Site).HasColumnName("theme_site").HasColumnType("nvarchar").HasMaxLength(50);
            modelBuilder.Entity<Theme>().Property(t => t.Description).HasColumnName("theme_description").HasColumnType("text");
            modelBuilder.Entity<Theme>().Property(t => t.Path).HasColumnName("theme_site").HasColumnType("nvarchar").HasMaxLength(500); ;
            #endregion

            #region PAGE Entity
            modelBuilder.Entity<Page>().HasKey(p => p.ID); 
            modelBuilder.Entity<Page>().Property(p => p.ID).HasColumnName("page_id").HasColumnType("int");
            modelBuilder.Entity<Page>().Property(p => p.UserID).HasColumnName("page_user_id").HasColumnType("int");
            modelBuilder.Entity<Page>().Property(p => p.Title).HasColumnName("page_title").HasColumnType("nvarchar").HasMaxLength(250);
            modelBuilder.Entity<Page>().Property(p => p.IsPublic).HasColumnName("page_is_public").HasColumnType("bit");
            modelBuilder.Entity<Page>().Property(p => p.Site).HasColumnName("page_site").HasColumnType("varchar").HasMaxLength(4);
            modelBuilder.Entity<Page>().Property(p => p.Keyword).HasColumnName("page_keyword").HasColumnType("nvarchar").HasMaxLength(500);
            modelBuilder.Entity<Page>().Property(p => p.Description).HasColumnName("page_description").HasColumnType("nvarchar").HasMaxLength(500);
            modelBuilder.Entity<Page>().Property(p => p.ParentID).HasColumnName("page_parent_id").HasColumnType("int").IsOptional() ;
            modelBuilder.Entity<Page>().Property(p => p.Url).HasColumnName("page_url").HasColumnType("nvarchar").HasMaxLength(500);
            modelBuilder.Entity<Page>().Property(p => p.Language).HasColumnName("page_language").HasColumnType("varchar").HasMaxLength(4);
            modelBuilder.Entity<Page>().Property(p => p.CreatedDate).HasColumnName("page_created_date").HasColumnType("datetime");
            modelBuilder.Entity<Page>().Property(p => p.ModifiedDate).HasColumnName("page_modified_date").HasColumnType("datetime");            
            modelBuilder.Entity<Page>().Property(p => p.LogID).HasColumnName("page_log_id").HasColumnType("int");
            #endregion

            #region MODULE Entity
            modelBuilder.Entity<Module>().HasKey(p => p.ID);
            modelBuilder.Entity<Module>().Property(p => p.ID).HasColumnName("module_id").HasColumnType("int");
            modelBuilder.Entity<Module>().Property(p => p.Position).HasColumnName("module_user_id").HasColumnType("int");
            modelBuilder.Entity<Module>().Property(p => p.Name).HasColumnName("module_name").HasColumnType("nvarchar").HasMaxLength(250);
            modelBuilder.Entity<Module>().Property(p => p.Description).HasColumnName("module_description").HasColumnType("nvarchar").HasMaxLength(500);
            modelBuilder.Entity<Module>().Property(p => p.Settings).HasColumnName("module_settings").HasColumnType("ntext");
            modelBuilder.Entity<Module>().Property(p => p.Views).HasColumnName("module_views").HasColumnType("nvarchar").HasMaxLength(500);
            modelBuilder.Entity<Module>().Property(p => p.Order).HasColumnName("module_parent_id").HasColumnType("int");
            modelBuilder.Entity<Module>().Property(p => p.NamespaceController).HasColumnName("module_namespace").HasColumnType("nvarchar").HasMaxLength(500);
            modelBuilder.Entity<Module>().Property(p => p.ContentTypeHelper).HasColumnName("module_typehelper").HasColumnType("nvarchar").HasMaxLength(500);
            #endregion

            #region PageModuleSettings Entity
            modelBuilder.Entity<PageModuleSettings>().HasKey(p => new {p.PageID,p.ModuleID});
            modelBuilder.Entity<PageModuleSettings>().Property(p => p.PageID).HasColumnName("page_id").HasColumnType("int");
            modelBuilder.Entity<PageModuleSettings>().Property(p => p.ModuleID).HasColumnName("module_id").HasColumnType("int");
            modelBuilder.Entity<PageModuleSettings>().Property(p => p.Position).HasColumnName("pm_position").HasColumnType("int");
            modelBuilder.Entity<PageModuleSettings>().Property(p => p.Order).HasColumnName("pm_order").HasColumnType("int");
            modelBuilder.Entity<PageModuleSettings>().Property(p => p.OwnSetting).HasColumnName("pm_own_setting").HasColumnType("ntext");
            #endregion


            base.OnModelCreating(modelBuilder);
            //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //modelBuilder.Entity<Instructor>()
            //    .HasOptional(p => p.OfficeAssignment).WithRequired(p => p.Instructor);
            //modelBuilder.Entity<Course>()
            //    .HasMany(c => c.Instructors).WithMany(i => i.Courses)
            //    .Map(t => t.MapLeftKey("CourseID")
            //        .MapRightKey("InstructorID")
            //        .ToTable("CourseInstructor"));
            //modelBuilder.Entity<Department>()
            //    .HasOptional(x => x.Administrator);
        }
    }
}
